import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout, getParentLayout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'
import { qiankunWindow } from 'vite-plugin-qiankun/dist/helper'
const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    name: 'Dashboard',
    redirect: '/index',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/home/index.vue'),
        name: 'home',
        meta: {
          title: t('router.dashboard'),
          icon: 'ant-design:home-filled'
        }
      }
    ]
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]
export const childrenRoute: AppRouteRecordRaw[] = [
  {
    path: '/system/menuManage/detail',
    component: () => import('@/views/System/MenuManage/detail.vue'),
    name: 'MenuDetail',
    meta: {
      hidden: true,
      title: t('router.menuDetail'),
      noTagsView: true,
      activeMenu: '/system/menuManage' // 显示的路由，也是动态添加的父路由
    }
  },
  {
    path: '/system/announcement/add',
    component: () => import('@/views/System/announcement/AnnouncementAdd.vue'),
    name: 'announcementAdd',
    meta: {
      hidden: true,
      title: t('router.announcementAdd'),
      noTagsView: true,
      activeMenu: '/system/announcement' // 显示的路由，也是动态添加的父路由
    }
  }
]
export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/home',
    component: Layout,
    name: 'Dashboard',
    redirect: '/home/index',
    meta: {},
    children: [
      {
        path: 'index',
        component: () => import('@/views/home/index.vue'),
        name: 'home',
        meta: {
          title: t('router.dashboard'),
          icon: 'ant-design:home-filled'
        }
      }
    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/userManage',
    name: 'System',
    meta: {
      title: t('router.system'),
      icon: 'ant-design:setting-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'userManage',
        component: () => import('@/views/System/UserManage/index.vue'),
        name: 'UserManage',
        meta: {
          title: t('router.userManage'),
          noCache: true,
          affix: true
        }
      },
      {
        path: 'departmentManage',
        component: () => import('@/views/System/DepartmentManage/index.vue'),
        name: 'DepartmentManage',
        meta: {
          title: t('router.departmentManage'),
          noCache: true,
          affix: true
        }
      },
      {
        path: 'roleManage',
        component: () => import('@/views/System/RoleManage/index.vue'),
        name: 'RoleManage',
        meta: {
          title: t('router.roleManage'),
          noCache: true,
          affix: true
        }
      },
      {
        path: 'menuManage',
        component: () => import('@/views/System/MenuManage/index.vue'),
        name: 'menuManage',
        meta: {
          title: t('router.menuManage'),
          noCache: true,
          affix: true
        },
        children: [
          {
            path: 'detail',
            component: () => import('@/views/System/MenuManage/detail.vue'),
            name: 'MenuDetail',
            meta: {
              title: t('router.menuDetail'),
              noTagsView: true,
              noCache: true,
              hidden: true,
              canTo: true,
              activeMenu: '/system/menuManage'
            }
          }
        ]
      },
      {
        path: 'dataDictionary',
        component: () => import('@/views/System/DataDictionary/index.vue'),
        name: 'dataDictionary',
        meta: {
          title: t('router.dataDictionary'),
          noCache: true,
          affix: true
        }
      },
      {
        path: 'classDictionary',
        component: () => import('@/views/System/ClassDictionary/index.vue'),
        name: 'classDictionary',
        meta: {
          title: t('router.classDictionary'),
          noCache: true,
          affix: true
        }
      },
      {
        path: 'searchBarManagement',
        component: () => import('@/views/System/SearchBarManagement/index.vue'),
        name: 'searchBarManagement',
        meta: {
          title: t('router.searchBarManagement'),
          noCache: true,
          affix: true
        }
      },
      {
        path: 'tableManagement',
        component: () => import('@/views/System/TableManagement/index.vue'),
        name: 'tableManagement',
        meta: {
          title: t('router.tableManagement'),
          noCache: true,
          affix: true
        }
      },
      {
        path: 'announcement',
        component: () => import('@/views/System/announcement/index.vue'),
        name: 'announcement',
        meta: {
          title: t('router.announcement'),
          noCache: true,
          affix: true
        },
        children: [
          {
            path: 'add',
            component: () => import('@/views/System/announcement/AnnouncementAdd.vue'),
            name: 'announcementAdd',
            meta: {
              title: t('router.announcementAdd'),
              noTagsView: true,
              noCache: true,
              hidden: true,
              canTo: true,
              activeMenu: '/system/announcement'
            }
          },
          {
            path: 'ad',
            component: () => import('@/views/System/announcement/AnAdd.vue'),
            name: 'AnAdd',
            meta: {
              title: t('router.announcementAdd'),
              noTagsView: true,
              noCache: true,
              hidden: true,
              canTo: true,
              activeMenu: '/system/announcement'
            }
          },
          {
            path: 'detail',
            component: () => import('@/views/System/announcement/AnnouncementDetail.vue'),
            name: 'announcementDetail',
            meta: {
              title: t('router.announcementDetail'),
              noTagsView: true,
              noCache: true,
              hidden: true,
              canTo: true,
              activeMenu: '/system/announcement'
            }
          }
        ]
      }
    ]
  },
  {
    path: '/supervisory',
    component: Layout,
    redirect: '/supervisory/onlineUser',
    name: 'supervisory',
    meta: {
      title: t('router.systemMonitoring'),
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'onlineUser',
        component: () => import('@/views/SysMonitor/OnlineUser/index.vue'),
        name: 'onlineUser',
        meta: {
          title: t('router.onlineUser'),
          noCache: true,
          affix: true
        }
      },
      // {
      //   path: 'dataLog',
      //   component: () => import('@/views/System/DataLog/index.vue'),
      //   name: 'DataLog',
      //   meta: {
      //     title: t('router.dataLog'),
      //     noCache: true,
      //     affix: true
      //   }
      // },
      {
        path: 'dataManage',
        component: () => import('@/views/System/DataManage/index.vue'),
        name: 'DataManage',
        meta: {
          title: t('router.dataManage'),
          noCache: true,
          affix: true
        }
      },
      {
        path: 'monitor',
        name: 'monitor',
        component: getParentLayout(),
        redirect: '/supervisory/monitor/redis',
        meta: {
          title: t('router.monitor'),
          alwaysShow: true
        },
        children: [
          {
            path: 'redis',
            component: () => import('@/views/SysMonitor/monitor/RedisInfo.vue'),
            name: 'redis',
            meta: {
              title: t('router.Redis')
            }
          },
          {
            path: 'systemInfo',
            component: () => import('@/views/SysMonitor/monitor/SystemInfo.vue'),
            name: 'systemInfo',
            meta: {
              title: t('router.systemInfo')
            }
          },
          {
            path: 'jvmInfo',
            component: () => import('@/views/SysMonitor/monitor/JVMInfo.vue'),
            name: 'jvmInfo',
            meta: {
              title: t('router.jvmInfo')
            }
          },
          {
            path: 'diskInfo',
            component: () => import('@/views/SysMonitor/monitor/diskInfo.vue'),
            name: 'diskInfo',
            meta: {
              title: t('router.diskInfo')
            }
          },
          {
            path: 'tomcatInfo',
            component: () => import('@/views/SysMonitor/monitor/TomcatInfo.vue'),
            name: 'tomcatInfo',
            meta: {
              title: t('router.tomcatInfo')
            }
          },
          {
            path: 'httpTrace',
            component: () => import('@/views/SysMonitor/monitor/HttpTrace.vue'),
            name: 'httpTrace',
            meta: {
              title: t('router.httpTrace')
            }
          }
        ]
      }
    ]
  }
]
const router = createRouter({
  history: qiankunWindow.__POWERED_BY_QIANKUN__
    ? createWebHistory('/opinion-sys/')
    : createWebHistory('/sub/opinion-sys/'),
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
